package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Login implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		HttpServletRequest servletRequest=(HttpServletRequest) arg0;
		HttpServletResponse servletResponse=(HttpServletResponse) arg1;
		if(servletRequest.getRequestURI().indexOf("login")!=-1||servletRequest.getRequestURI().indexOf("regist")!=-1||servletRequest.getRequestURI().indexOf("myjsp")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("passwordback")!=-1||servletRequest.getRequestURI().indexOf("checkuser")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/email")!=-1||servletRequest.getRequestURI().indexOf("products/checkIdCard")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/regcheckname")!=-1||servletRequest.getRequestURI().indexOf("products/createVCode")!=-1||servletRequest.getRequestURI().indexOf("products/checkVCode")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/acc")!=-1||servletRequest.getRequestURI().indexOf("products/resendregemail")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/checkanswer")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/setnewpwdcfr")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/passwordBackByEmail")!=-1||servletRequest.getRequestURI().indexOf("products/checkEmailCode")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/news")!=-1||servletRequest.getRequestURI().indexOf("products/newsDetail")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/recharge")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/customerservicecenter")!=-1||servletRequest.getRequestURI().indexOf("products/addQuestion")!=-1||servletRequest.getRequestURI().indexOf("products/getReplyContent")!=-1||servletRequest.getRequestURI().indexOf("products/addReply")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/realNameInfoSub")!=-1||servletRequest.getRequestURI().indexOf("products/sendComplaint")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/regcheckphone")!=-1||servletRequest.getRequestURI().indexOf("products/regsmscode")!=-1||servletRequest.getRequestURI().indexOf("products/checksmscode")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		if(servletRequest.getRequestURI().indexOf("products/rules")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		
		if(servletRequest.getRequestURI().indexOf("products/getServerByGame")!=-1||servletRequest.getRequestURI().indexOf("products/getRolesByServer")!=-1){
			arg2.doFilter(arg0, arg1);
			return;
		}
		String temp[]=servletRequest.getRequestURI().split("\\.");
		if(temp.length>1){
			if(temp[1].equals("js")||temp[1].equals("css")||temp[1].equals("jpg")||temp[1].equals("png")||temp[1].equals("gif")||temp[1].equals("exe")||temp[1].equals("ico")){
				arg2.doFilter(arg0, arg1);
				return;
			}
		}
		if(servletRequest.getRequestURI().equals("/products/checkpwd")||servletRequest.getRequestURI().equals("/products/index")||servletRequest.getRequestURI().equals("/products/")){
			arg2.doFilter(arg0, arg1);
			return;
		}else{
			if(servletRequest.getSession().getAttribute("name")!=""&&servletRequest.getSession().getAttribute("name")!=null){
				arg2.doFilter(arg0, arg1);
				return;
			}else{
				arg0.getRequestDispatcher("login.jsp").forward(arg0, arg1);
				return;
			}
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}

}
